package com.stepshop.sys.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.oracle.jrockit.jfr.RequestableEvent;
import com.stepshop.sys.bean.SysArea;
import com.stepshop.sys.service.SysAreaService;
import com.stepshop.wechat.utils.HttpClientUtil;

@Controller
@RequestMapping("/sysArea")
public class SysAreaController {
	Logger logger=Logger.getLogger(this.getClass());
	Map map=new HashMap<String, Object>();
	@Resource 
	private SysAreaService areaService;
	@RequestMapping("/save")
	@ResponseBody
	public String addSysArea(String areaForm,HttpServletRequest requeste,HttpServletResponse response){
		logger.info("areaForm:"+areaForm);
		SysArea sysArea = JSON.parseObject(areaForm, SysArea.class);
		sysArea.setCode(System.currentTimeMillis());
		areaService.saveArea(sysArea);
		System.out.println(1111111);
		map.put("status", "SUCCESS");
		return JSON.toJSONString(map);
	}
	@RequestMapping("/getMap")
	@ResponseBody
	public Map getSysArea(String areaForm,HttpServletRequest requeste,HttpServletResponse response){
		System.out.println(1111111);
		map.put("status", "呵呵");
		return map;
	}
	@RequestMapping("/getString")
	@ResponseBody
	public String getSysAreaStr(String areaForm,HttpServletRequest requeste,HttpServletResponse response){
		System.out.println(1111111);
		map.put("status", "呵呵");
		return JSON.toJSONString(map);
	}
	@RequestMapping(value="fileUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public String upload(HttpServletRequest request,HttpServletResponse response,MultipartFile file){
		return "";
	}
	@RequestMapping(value="download")
	public void download(HttpServletRequest request,HttpServletResponse response){
		try {
			String realPath = request.getSession().getServletContext().getRealPath("/file");
			File file=new File(realPath+"/test.mp4");
			InputStream is=new FileInputStream(file);
			RandomAccessFile raFile=new RandomAccessFile(file,"r");
			String range = request.getHeader("RANGE");
			ServletOutputStream sos = response.getOutputStream();
			response.setHeader("content-disposition", "attachment;filename="+new Date().getTime()+".mp4");
			byte[] b=new byte[1024];
			int len=0;
			raFile.seek(0l);
			while((len=raFile.read(b))!=-1){
				sos.write(b, 0, len);
			}
//			while((len=is.read(b))!=-1){
//				sos.write(b, 0, len);
//			}
			sos.close();
			raFile.close();
//			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

